<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/User for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace User\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use User\Form\LoginForm;
use User\Form\RegisterForm;
use User\Model\Users;
use User\Model\UsersTable;
use Zend\Barcode\Barcode;

class UserController extends AbstractActionController
{
    protected $userTable;
    
    public function indexAction()
    {
//         $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
        
//         $renderOptions = array();
//         $imageResource = Barcode::factory('code39', 'image', $barcodeOptions, $renderOptions)->render();
//         echo $imageResource;
//         Barcode::render('code39', 'pdf', array('text'=> 'ZEND-FRAMEWORK'));
        return array();
    }
    
    public function registerAction()
    {
    	$form = new RegisterForm();
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		$user = new Users();
    		$form->setInputFilter($user->getInputFilter());
    		$form->setData($request->getPost());
    		if ($form->isValid()) {
    			$user->exchangeArray($form->getData());
    			$this->getUserTable()->saveUser($user);
    			
    			return $this->redirect()->toRoute('user');
    		} else {
    			var_dump($form->getData());
    		}
    	}
    	return array('form' => $form);
    }

    public function loginAction()
    {
        $form = new LoginForm();
    	$form->get('submit')->setValue('Add');
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		;
    	}
    	return array('form' => $form);
    }
    
    public function fooAction()
    {
    	
    }
    
    /**
     * 
     * @return UsersTable
     */
    protected function getUserTable()
    {
        if (!$this->userTable) {
        	$sm = $this->getServiceLocator();
        	$this->userTable = $sm->get('User\Model\UsersTable');
        }
        return $this->userTable;
    }
}
